import { setting } from '@/config/default'
import sysConfig from '@/config/config'
import fastEqual from 'fast-deep-equal'
import deepMerge from 'deepmerge'
import store from '@/store'

export function initSetting(local=false){
  let config = {}
  let mySetting = store.state.setting
  let dftSetting = local ? deepMerge(setting, sysConfig) : setting
  Object.keys(mySetting).forEach(key => {
    const dftValue = dftSetting[key], myValue = mySetting[key]
    if (dftValue != undefined && !fastEqual(dftValue, myValue)) {
      config[key] = myValue
    }
  })
  return config
}

export function saveSetting(){
  const config = initSetting(true)
  localStorage.setItem(process.env.VUE_APP_SETTING_KEY, JSON.stringify(config))
}